#ifndef _TSH_HPP
#define _TSH_HPP 1

#include "get_command.hpp"
#include "prompt.hpp"
#include "run_command.hpp"
#include "sig.hpp"

static constexpr char DPS[] = "Tsh > ";
class Tsh {
  Prompt ps;
  std::vector<std::string> pathLexem;
  umap<std::string, std::string> aliasTable;
  int lastCode;

  Tsh& SignalLoad() {
    signal(SIGINT, ACT_SIGINT);
    signal(SIGTSTP, ACT_SIGTSTP);
    return *this;
  }

 public:
  Tsh() : ps(DPS), pathLexem(), lastCode(0), aliasTable() {
    // get path array
    Base::obj(getpid());
  }

  void run() {
    std::ios_base::sync_with_stdio(false);
    int Go = 0;
    SignalLoad();
    do {
      std::cout << ps.get() << std::flush;
      // std::cin.setstate(std::ios_base::goodbit);
      Go = doIt(get(), true);
#ifdef DEBUG
      std::cout << "Go:" << Go << std::endl;
#endif
    } while (Go != INF);
  }
  int doIt(std::string const& cmd, bool c = false) {
    return CommandRun(cmd, c);
  }
  std::string get() { return getCmd(Type::Line); }
  std::string getPath() noexcept {
    char* tmp;
    tmp = get_current_dir_name();
    return std::string(tmp);
  }
};
#endif  // SRC/TSH_HPP
